package com.huabo.system.service.impl;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.mysql.entity.FindMySql;
import com.huabo.system.mysql.entity.TblExternalExpertMySql;
import com.huabo.system.mysql.entity.TblOrganizationMySql;
import com.huabo.system.mysql.mapper.TblExternalExpertMySqlDao;
import com.huabo.system.mysql.mapper.TblOrganizationMySqlMapper;
import com.huabo.system.oracle.entity.Find;
import com.huabo.system.oracle.entity.TblExternalExpert;
import com.huabo.system.oracle.entity.TblOrganization;
import com.huabo.system.oracle.mapper.TblExternalExpertDao;
import com.huabo.system.oracle.mapper.TblOrganizationMapper;
import com.huabo.system.service.TblExternalExpertService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class TblExternalExpertServiceImpl implements TblExternalExpertService {

    @Resource
    private TblExternalExpertDao tblExternalExpertDao;

    @Resource
    private TblOrganizationMapper tblOrganizationMapper;


    @Resource
    private TblExternalExpertMySqlDao tblExternalExpertMySqlDao;

    @Resource
    private TblOrganizationMySqlMapper tblOrganizationMySqlMapper;


    @Override
    public TblExternalExpert getExpert(BigDecimal exterid) {
        return tblExternalExpertDao.get(exterid);
    }

    @Override
    public TblExternalExpertMySql getMySqlExpert(BigDecimal exterid) {
        return tblExternalExpertMySqlDao.get(exterid);
    }

    @Override
    public Map<String, Object> findByOrgId(Find find, Integer pageNumber, Integer pageSize, String token,
                                           String staffId, String company) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        Map<String, Object> dataMap = new HashMap<String, Object>(0);
        try {

            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            TblOrganization org = tblOrganizationMapper.findByWPZJK(orgid, TblOrganization.WPZJK);
            PageInfo<TblExternalExpert> pageInfo = new PageInfo<TblExternalExpert>();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTlist(tblExternalExpertDao.selectTblExternal(pageInfo, orgid, find, company));
            pageInfo.setTotalRecord(tblExternalExpertDao.selectExternal(orgid, find, company));
            dataMap.put("org", org);
            dataMap.put("pageInfo", pageInfo);
            resultMap.put("data", dataMap);


            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;

    }

    @Override
    public Map<String, Object> findByMySqlOrgId(Find find, Integer pageNumber, Integer pageSize, String token, String staffId, String company) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        Map<String, Object> dataMap = new HashMap<String, Object>(0);
        try {

            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            TblOrganizationMySql org = tblOrganizationMySqlMapper.findByWPZJK(orgid, TblOrganization.WPZJK);
            PageInfo<TblExternalExpertMySql> pageInfo = new PageInfo<TblExternalExpertMySql>();
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTlist(tblExternalExpertMySqlDao.selectTblExternal(pageInfo, orgid, find, company));
            pageInfo.setTotalRecord(tblExternalExpertMySqlDao.selectExternal(orgid, find, company));
            dataMap.put("org", org);
            dataMap.put("pageInfo", pageInfo);
            resultMap.put("data", dataMap);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public void updateTblExternalExpert(TblExternalExpert tee) {
        tblExternalExpertDao.updateTblExternalExpert(tee);
    }

    @Override
    public void updateMySqlTblExternalExpert(TblExternalExpertMySql teeMySql) {
        tblExternalExpertMySqlDao.updateTblExternalExpert(teeMySql);
    }

    @Override
    public void saveTblExternalExpert(TblExternalExpert tee) {
        tblExternalExpertDao.saveTblExternalExpert(tee);
    }

    @Override
    public void saveMySqlTblExternalExpert(TblExternalExpertMySql teeMySql) {
        tblExternalExpertMySqlDao.saveTblExternalExpert(teeMySql);
    }

//    @Override
//    public Map<String, Object> findByOrgId(Find find, Integer pageNumber,Integer pageSize,String token) {
//        find.getUserName();
//        Map<String,Object> resultMap = new HashMap<String, Object>(0);
//        PageInfo<TblExternalExpert> pageInfo = new PageInfo<TblExternalExpert>();
//        pageInfo.setCurrentPage(pageNumber);
//        pageInfo.setPageSize(pageSize);
//        Claims claims = JwtUtils.parseJwt(token);
//        Object object = claims.get("staffInfo");
//        JSONObject objJson = JSONObject.fromObject(object);
//        TblStaff staff = (TblStaff) JSONObject.toBean(objJson,TblStaff.class);
//        BigDecimal orgid = staff.getCurrentOrg().getOrgid();
//        pageInfo.setTlist(tblExternalExpertDao.selectListByPageInfoo(find.getUserName()));
//        resultMap.put("code", "1");
//        resultMap.put("msg", "访问接口成功");
//        resultMap.put("data", pageInfo);
//        return resultMap;
//    }
}
